<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Gallery extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('parser');		
	}

	function index() {
		$this->load->helper('options');

		//set title and meta description
		$this->parser->set_title('Gellery');
		$this->parser->set_desc('Index gallery Pilar Teknotama');

		//model
		$this->load->model('m_album');

		//ambil berita dan create paging
		$this->load->library('paging');                         	
		$config['sql']      				= $this->m_album->get();
		$config['per_page'] 				= 24;
		$data = $this->paging->create_pagination($config);

		$this->parser->theme('front','gallery/index', $data);
	}

	function detail_gallery(){
		$this->load->helper('options');
		$this->load->helper('id_date');

		//ambil data uri
		$slug2							=	$this->uri->segment('2');

		if(!empty($slug2) && $this->cek_slug($slug2)){
			$id_album 				= $this->get_album_id($slug2);

			//model
			$this->load->model('m_album');
			$this->load->model('m_gallery');

			//ambil berita
			$data['album'] 		= $this->m_album->get_by_slug($slug2);
			$data['gallery'] 	= $this->m_gallery->get_by_album_id($id_album);

			//potong kalimat per kata dan hilangkan tag html
			$this->load->helper('trim_text');
			//set title and meta description
			$this->parser->set_title($data['album']['album']);
			$this->parser->set_desc('Gallery foto dari album' . $data['album']['album']);

			$this->parser->theme('front','gallery/detail_gallery', $data);

		}else{
			show_404();
		}
	}

	//untuk cek slug lesson, apakah ada dalam tabel lesson
	//private
	//bool
	private function cek_slug($slug){
		if (empty($slug))
		{
			return FALSE;
		}

		return $this->db->where('slug', $slug)
				->order_by("id_album", "ASC")
				->limit(1)
		    ->count_all_results('album') > 0;
	}

	//ambil id_layanan berdasarkan slug
	//private
	private function get_album_id($slug){
		if (empty($slug))
		{
			return FALSE;
		}

		$this->db->select('id_album');
		$query = $this->db->get_where('album', array('slug' => $slug), 1);
		if ($query->num_rows() > 0) {
      $result = $query->row_array();
      $query->free_result();
    } else {
      $result = array();
    }
    return $result['id_album'];
	}
}